//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zxl.yjb.activity.im;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMap.OnCameraChangeListener;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.zxl.yjb.R;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.common.RLog;
import io.rong.imkit.R.string;

public class MapLocationActivity extends BaseActivity implements AMap.OnMyLocationChangeListener, OnCameraChangeListener, OnGeocodeSearchListener {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    @BindView(R.id.rc_ext_location_marker)
    TextView mLocationTip;

    @BindView(R.id.map_view)
    MapView mAMapView;

    private AMap aMap;
    private MyLocationStyle myLocationStyle;
    private Marker mMarker;
    private GeocodeSearch mGeocodeSearch;
    private BitmapDescriptor mBitmapDescriptor;
    private double mLatResult;
    private double mLngResult;
    private String mPoiResult;
    private double mMyLat;
    private double mMyLng;
    private String mMyPoi;
    private LatLng latLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plugin_location_activity);
        ButterKnife.bind(this);
        initView();
        initMap(savedInstanceState);

    }

    @Override
    protected void initView() {
        tv_confirm.setVisibility(View.VISIBLE);
        tv_confirm.setText("发送");
        tv_page_title.setText("选择接单位置");
    }


    private void initMap(Bundle savedInstanceState) {
        mAMapView.onCreate(savedInstanceState);// 此方法必须重写
        aMap = mAMapView.getMap();

        myLocationStyle = new MyLocationStyle();
        myLocationStyle.interval(2000);
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);
        aMap.setMyLocationStyle(myLocationStyle);
        //aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        aMap.setOnMyLocationChangeListener(this);
        aMap.setOnCameraChangeListener(this);
//        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        this.mGeocodeSearch = new GeocodeSearch(this);
        this.mGeocodeSearch.setOnGeocodeSearchListener(this);
    }


    @OnClick({R.id.iv_back, R.id.rc_ext_my_location, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rc_ext_my_location:
                aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng , 10));

                break;
            case R.id.tv_confirm:

                if (mLatResult == 0.0D && mLngResult== 0.0D && TextUtils.isEmpty(mPoiResult)) {
                    ToastUtils.show(MapLocationActivity.this,getString(string.rc_location_temp_failed));
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("thumb", getMapUrl(mLatResult, mLngResult));
                    intent.putExtra("lat", mLatResult);
                    intent.putExtra("lng", mLngResult);
                    intent.putExtra("poi", mPoiResult);
                    setResult(-1, intent);
                    finish();
                }
                break;
        }
    }

    @Override
    public void onMyLocationChange( Location locationInfo) {
        RLog.d("MapLocationActivity", "onLocationChanged");
        if (locationInfo == null) {
            ToastUtils.show(getApplicationContext(),getString(R.string.rc_location_fail));
            return;
        }
       this.mMyLat=mLatResult = locationInfo.getLatitude();
       this.mMyLng=mLngResult = locationInfo.getLongitude();
       this.mMyPoi=mPoiResult = "";
        if (locationInfo instanceof AMapLocation) {
            AMapLocation aMapLocation = (AMapLocation) locationInfo;
            this.mMyPoi= mPoiResult = aMapLocation.getAddress();
        }
        latLng = new LatLng(this.mLatResult, this.mLngResult);
        addLocatedMarker(latLng, this.mPoiResult);
        
    }

    @Override
    public void onCameraChange(CameraPosition cameraPosition) {

    }

    @Override
    public void onCameraChangeFinish(CameraPosition cameraPosition) {

        LatLonPoint point = new LatLonPoint(cameraPosition.target.latitude, cameraPosition.target.longitude);
        RegeocodeQuery query = new RegeocodeQuery(point, 50.0F, "autonavi");
        this.mGeocodeSearch.getFromLocationAsyn(query);
    }
    private String getMapUrl(double latitude, double longitude) {
        return "http://restapi.amap.com/v3/staticmap?location=" + longitude + "," + latitude + "&zoom=16&scale=2&size=408*240&markers=mid,,A:" + longitude + "," + latitude + "&key=e09af6a2b26c02086e9216bd07c960ae";
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        if (regeocodeResult != null) {
            RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
            this.mLatResult = regeocodeResult.getRegeocodeQuery().getPoint().getLatitude();
            this.mLngResult = regeocodeResult.getRegeocodeQuery().getPoint().getLongitude();
            String formatAddress = regeocodeResult.getRegeocodeAddress().getFormatAddress();
            this.mPoiResult = formatAddress.replace(regeocodeAddress.getProvince(), "").replace(regeocodeAddress.getCity(), "").replace(regeocodeAddress.getDistrict(), "");
            mLocationTip.setText(this.mPoiResult);

//            LatLng latLng = new LatLng(this.mLatResult, this.mLngResult);
        } else {
            ToastUtils.show(getApplicationContext(),getString(R.string.rc_location_fail));
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }
    private void addLocatedMarker(LatLng latLng, String poi) {
        this.mBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.zy_dtdw);
        MarkerOptions markerOptions = (new MarkerOptions()).position(latLng).icon(this.mBitmapDescriptor);
        this.mMarker = this.aMap.addMarker(markerOptions);
        this.mMarker.setPositionByPixels(this.mAMapView.getWidth() / 2, this.mAMapView.getHeight() / 2);
        this.mLocationTip.setText(String.format("%s", poi));
    }
}
